package proyecto.testcasetool.presentation.details.scenario;

import org.eclipse.core.databinding.ObservablesManager;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.databinding.edit.EMFEditObservables;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import proyecto.testcasetool.TestcasetoolPackage;
import proyecto.testcasetool.presentation.details.DetailsPage;
import proyecto.testcasetool.presentation.editor.pages.FormLayoutFactory;


public abstract class AbstractScenarioDetailsPage extends DetailsPage  {

	protected Table stepsTable;
	protected Text txtFlowName;
	protected Text txtDescription;
	
	protected ObservablesManager obsManager;
	protected EMFDataBindingContext bindingContext;
	
	public AbstractScenarioDetailsPage(FormToolkit toolkit, EditingDomain editingDomain,
			AdapterFactory adapterFactory) {
		super(toolkit, editingDomain, adapterFactory);
		
		bindingContext = new EMFDataBindingContext();
		obsManager = new ObservablesManager();
		
	}
	
	
	@Override
	protected void createDetailsSection(Composite parent) {
		GridLayout gridLayout = (GridLayout) parent.getLayout();
		gridLayout.verticalSpacing = 5;

		Section s = toolkit.createSection(parent, Section.TITLE_BAR);
		s.setText("Flow details");
		s.setLayoutData(new GridData(GridData.FILL_BOTH));
		s.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite client = toolkit.createComposite(s,SWT.FILL);
		
		s.setClient(client);
		client.setLayout(new GridLayout(2, false));
		toolkit.paintBordersFor(client);
		
		Label lblName = toolkit.createLabel(client, "Name", SWT.NONE);
		GridData gd_lblName = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblName.verticalIndent = 2;
		lblName.setLayoutData(gd_lblName);
		
		txtFlowName = toolkit.createText(client, "New Text", SWT.NONE);
	
		GridData gd_txtBasicFlowName = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_txtBasicFlowName.verticalIndent = 2;
		gd_txtBasicFlowName.widthHint = 244;
		txtFlowName.setLayoutData(gd_txtBasicFlowName);
		
		Label lblDescription = toolkit.createLabel(client, "Description", SWT.NONE);
		txtDescription = toolkit.createText(client, "", SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		
		GridData gd_txtDescription = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_txtDescription.heightHint = 80;
		gd_txtDescription.verticalIndent = 10;
		gd_txtDescription.widthHint = 0;
		txtDescription.setLayoutData(gd_txtDescription);
		
	}
	
	
	protected void doBinding() {
		IObservableValue obsDescFeature = EMFEditObservables
				.observeValue(editingDomain, eObject,
						TestcasetoolPackage.Literals.SCENARIO__DESCRIPTION);

		
		bindingContext.bindValue(SWTObservables.observeText(
				txtDescription, SWT.FocusOut), obsDescFeature);

		
		IObservableValue obsNameFeature = EMFEditObservables
				.observeValue(editingDomain, eObject,
						TestcasetoolPackage.Literals.SCENARIO__NAME);

		bindingContext.bindValue(
				SWTObservables.observeDelayedValue(400, SWTObservables
						.observeText(txtFlowName, SWT.Modify)),
				obsNameFeature);
		
		
		obsManager.addObservable(obsNameFeature);
		obsManager.addObservable(obsDescFeature);
	}
	
	
	@Override
	public void dispose() {
		super.dispose();
		obsManager.dispose();
	}
}
